(function(window) {

	SolarSystem.elements = [];
	SolarSystem.starElements = [];
	SolarSystem.planetNames = [ "mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto" ];
	SolarSystem.celestialBodies = [ "sun", "mercury", "venus", "moon", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto" ];
	SolarSystem.solarSystemBodies = [ "bg", "sun", "mercury", "venus", "earth", "moon", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto" ];
	SolarSystem.canvasImages = [];
	SolarSystem.compassImagesCount = 4;
	SolarSystem.compassImages = {};
	SolarSystem.canvasElements = [];
	SolarSystem.canvas = null;
	SolarSystem.canvasContext = null;
	SolarSystem.starCanvasElements = [];
	SolarSystem.starImage = null;

	var canvasImageLoadCount = 0;
	var imageLoadCount = 0;
	var bgImage = null;

	var _onLoadComplete = function(){};

	SolarSystem.prototype.setOnLoadComplete = function(f){
		_onLoadComplete = f;
	}
	function imageLoaded(event){
		imageLoadCount ++;
		if(imageLoadCount == SolarSystem.solarSystemBodies.length){
			_onLoadComplete();
		}

		
		var sse = SolarSystem.getElement(event.target.parentNode.id)
		sse.width = event.target.width;
		//event.target.style.top = -(event.target.width >> 1);
		//event.target.style.left = -(event.target.width >> 1);
	}
	function circularOrbitImageLoaded(event){
		imageLoadCount ++;
		if(imageLoadCount == SolarSystem.solarSystemBodies.length){
			_onLoadComplete();
		}

		
		var sse = SolarSystem.getElement(event.target.parentNode.id)
		sse.width = event.target.width;
		//event.target.style.top = -(event.target.width >> 1);
		//event.target.style.left = -(event.target.width >> 1);
	}

	function SolarSystem(){};

	//SolarSystem.prototype.makeElements = function(container, imagesPath){
		// if(!imagesPath) imagesPath = "images/";
		// this.container = container;
		// var ss = document.createElement("DIV");
		// ss.id = "solar_system";
		// for(var i in SolarSystem.solarSystemBodies){
		// 	var p = document.createElement("DIV");
		// 	p.setAttribute("id", SolarSystem.solarSystemBodies[i]);
		// 	p.setAttribute("class", "unselectable");
		// 	p.setAttribute("unselectable", "on");
		// 	var img = document.createElement("IMG");
		// 	img.setAttribute("class", "planetImage");
		// 	img.setAttribute("src", imagesPath + SolarSystem.solarSystemBodies[i] + ".png");
		// 	img.style.cursor = 'pointer'; 
		// 	img.onload = imageLoaded;
		// 	p.appendChild(img);
		// 	ss.appendChild(p);
		// 	SolarSystem.elements.push(new PlanetElement());//p, SolarSystem.solarSystemBodies[i]));
		// }
		// container.appendChild(ss);
	//}
	SolarSystem.prototype.makeCircularOrbitElements = function(container, imagesPath){
		if(!imagesPath) imagesPath = "images/";
		this.container = container;
		var ss = document.createElement("DIV");
		ss.id = "solar_system";
		for(var i in SolarSystem.solarSystemBodies){
			var p = document.createElement("DIV");
			p.setAttribute("id", SolarSystem.solarSystemBodies[i]);
			p.setAttribute("class", "unselectable");
			p.setAttribute("unselectable", "on");
			var img = document.createElement("IMG");
			img.setAttribute("class", "planetImage");
			img.setAttribute("src", imagesPath + SolarSystem.solarSystemBodies[i] + ".png");
			img.style.cursor = 'pointer'; 
			img.onload = circularOrbitImageLoaded;
			p.appendChild(img);
			ss.appendChild(p);
			SolarSystem.elements.push(new PlanetElement());//p, SolarSystem.solarSystemBodies[i]));
			
		}
		container.appendChild(ss);
	}
	SolarSystem.prototype.makeHitElements = function(container){
		//this.container = container;
		//var ss = document.createElement("DIV");
		//ss.id = "solar_system";
		 for(var i in SolarSystem.solarSystemBodies){
			// var p = document.createElement("DIV");
			// p.setAttribute("id", SolarSystem.solarSystemBodies[i]);
			// p.setAttribute("type", "planet");
			// p.setAttribute("class", "unselectable");
			// p.setAttribute("unselectable", "on");
			// p.style.cursor = 'pointer';
			var w = 20;
			if(SolarSystem.canvasElements[i].name != "moon"){
				if(SolarSystem.canvasElements[i].name == SolarSystem.solarSystemBodies[i] && SolarSystem.canvasElements[i].width > 0){
					//console.log("hey you! - "+SolarSystem.canvasElements[i].width)
					w = SolarSystem.canvasElements[i].width
				} else {
					w = 40;
				}
			} else {
				w = 38;
			}
			// p.style.width = w;
			// p.style.height = w;
			// ss.appendChild(p);
			var pe = new PlanetElement(null, SolarSystem.solarSystemBodies[i]);//p, SolarSystem.solarSystemBodies[i])
			pe.type = "planet"
			//pe.width = w;
			SolarSystem.elements.push(pe);
		 }
		//container.appendChild(ss);
	}
	SolarSystem.prototype.positionElements = function(){
		// for(var i in SolarSystem.elements){
		// 	if(SolarSystem.elements[i].name != "earth"){
		// 		SolarSystem.elements[i].element.style.left = 500+(SolarSystem.elements[i].x-(SolarSystem.elements[i].width >> 1));
		// 		SolarSystem.elements[i].element.style.top = 460+(SolarSystem.elements[i].y-(SolarSystem.elements[i].width >> 1));
		// 		SolarSystem.elements[i].element.style.zIndex = SolarSystem.elements[i].z;
		// 	}
		// }
	}
	SolarSystem.prototype.positionOrbitingElements = function(){
		for(var i in SolarSystem.elements){
			SolarSystem.elements[i].element.style.left = 500+(SolarSystem.elements[i].x-(SolarSystem.elements[i].width >> 1));
			SolarSystem.elements[i].element.style.top = 460+(SolarSystem.elements[i].y-(SolarSystem.elements[i].width >> 1));
			SolarSystem.elements[i].element.style.zIndex = SolarSystem.elements[i].z;
		}
	}
	SolarSystem.prototype.makeCanvas = function(container, width, height, imagesPath){
		if(!imagesPath) imagesPath = "images/";
		var canvas = SolarSystem.canvas = document.createElement("CANVAS");
		canvas.id = "solarSystemCanvas";
		canvas.width = width;
		canvas.height = height;
		SolarSystem.canvasContext = canvas.getContext("2d");
		for(var i in SolarSystem.solarSystemBodies){
			var p = new Image();
			p.id = SolarSystem.solarSystemBodies[i];
			p.onload = canvasImageLoaded;
			if(SolarSystem.solarSystemBodies[i] == "moon"){
				p.src = imagesPath+"moon_phases_38.png";
			} else if(SolarSystem.solarSystemBodies[i] == "bg") {
				p.src = imagesPath+"bgFine.png";
				bgImage = p
			} else {
				p.src = imagesPath+SolarSystem.solarSystemBodies[i]+".png";
			}
			var pe = new PlanetElement(p, SolarSystem.solarSystemBodies[i])
			SolarSystem.canvasElements.push(pe);
			SolarSystem.canvasImages.push(p);
			

		}
		var n = new Image();
		n.id = "north";
		n.src = "images/n.png"
		n.onload = canvasImageLoaded;
		SolarSystem.compassImages.n = n;
		var e = new Image();
		e.id = "east";
		e.src = "images/e.png"
		e.onload = canvasImageLoaded;
		SolarSystem.compassImages.e = e;
		var s = new Image();
		s.id = "south";
		s.src = "images/s.png"
		s.onload = canvasImageLoaded;
		SolarSystem.compassImages.s = s;
		var w = new Image();
		w.id = "west";
		w.src = "images/w.png"
		w.onload = canvasImageLoaded;
		SolarSystem.compassImages.w = w;
		
		var star = new Image();
		star.id = "star";
		star.src = imagesPath+"star8.png";
		star.onload = canvasImageLoaded;
		SolarSystem.starImage = star;
		SolarSystem.canvasImages.push(star);
		container.appendChild(canvas);
	}
	function SortByZ(a,b){ 
		return a.z - b.z; 
	}

	SolarSystem.prototype.positionCanvasElements = function(date){
		for (var s in SolarSystem.starCanvasElements){
			var AA = PlanetaryPositions.azimuthAltitude(SolarSystem.starCanvasElements[s].RA, SolarSystem.starCanvasElements[s].Dec, date);
			var TL = PlanetsAnimation.getTopLeft(AA.azimuth, AA.altitude);
			var w = SolarSystem.starImage.width
			if(SolarSystem.starCanvasElements[s].VM > 0){ 
				w = SolarSystem.starImage.width*Math.pow(0.7, SolarSystem.starCanvasElements[s].VM);
			}
			var x = (SolarSystem.canvas.width/2)+(TL.left-(w/2))
			var y = (SolarSystem.canvas.height/2)+(TL.top-(w/2))
			SolarSystem.canvasContext.drawImage(SolarSystem.starImage, x, y, w, w);
		}
		var a = SolarSystem.canvasElements.concat();
		a.sort(SortByZ)
		for(var i = 0; i<a.length; ++i){
			if(a[i].name != "bg" && a[i].name != "earth" && a[i].name != "moon"){
				SolarSystem.canvasContext.drawImage(a[i].element, (SolarSystem.canvas.width/2)+(a[i].x-(a[i].element.width/2)), (SolarSystem.canvas.height/2)+(a[i].y-(a[i].element.height/2)));
			} else if (a[i].name == "moon"){
				var moonSize = a[i].element.width/30;
				var x = (SolarSystem.canvas.width/2)+(a[i].x-(moonSize/2));
				var y = (SolarSystem.canvas.height/2)+(a[i].y-(moonSize/2));
				var xOff = moonSize*a[i].phase;
				SolarSystem.canvasContext.drawImage(a[i].element, xOff, 0, moonSize, moonSize, x, y, moonSize, moonSize);
			}
		}
	}
	SolarSystem.prototype.drawCanvasBG = function(){
		SolarSystem.canvasContext.clearRect(0, 0, SolarSystem.canvas.width, SolarSystem.canvas.height)
		var bgPosX = (SolarSystem.canvas.width-bgImage.width)/2;
		var bgPosY = (SolarSystem.canvas.height-bgImage.height)/2;
		SolarSystem.canvasContext.drawImage(bgImage, bgPosX, bgPosY);
		
		var nPosX = SolarSystem.canvas.width/2 - (SolarSystem.compassImages.n.height/2);
		var nPosY = (SolarSystem.canvas.height/2) + (((720/2) )*PlanetsAnimation.POLE);
		SolarSystem.canvasContext.drawImage(SolarSystem.compassImages.n, nPosX, nPosY - (SolarSystem.compassImages.n.height/2));
		
		var ePosX = ((SolarSystem.canvas.width/2) + ((720/2)*PlanetsAnimation.POLE));
		var ePosY = SolarSystem.canvas.height/2  - (SolarSystem.compassImages.e.width/2);
		SolarSystem.canvasContext.drawImage(SolarSystem.compassImages.e, ePosX - (SolarSystem.compassImages.e.width/2), ePosY);

		var sPosX = SolarSystem.canvas.width/2 - (SolarSystem.compassImages.s.height/2);
		var sPosY = (SolarSystem.canvas.height/2) - (((720/2))*PlanetsAnimation.POLE) ;
		SolarSystem.canvasContext.drawImage(SolarSystem.compassImages.s, sPosX, sPosY- (SolarSystem.compassImages.s.height/2));
		
		var wPosX = ((SolarSystem.canvas.width/2) - ((720/2)*PlanetsAnimation.POLE));
		var wPosY = SolarSystem.canvas.height/2 - (SolarSystem.compassImages.w.width/2);
		SolarSystem.canvasContext.drawImage(SolarSystem.compassImages.w, wPosX - (SolarSystem.compassImages.w.width/2), wPosY);
	}
	
	function canvasImageLoaded(event){
		console.log(event.target.id)
		canvasImageLoadCount ++;
		if(canvasImageLoadCount == SolarSystem.canvasImages.length+SolarSystem.compassImagesCount){
			_onLoadComplete();
		}
		if(SolarSystem.getCanvasElement(event.target.id)) SolarSystem.getCanvasElement(event.target.id).width = event.target.width
	}
	SolarSystem.getCanvasElement = function(id) {
		for(var i in SolarSystem.canvasElements){
			if(SolarSystem.canvasElements[i].name == id) return SolarSystem.canvasElements[i]
		}
		return null;
	};
	SolarSystem.getElement = function(id) {
		
		for(var i in SolarSystem.elements){
			if(SolarSystem.elements[i].name == id) return SolarSystem.elements[i]
		}
		return null;
	};
	window.SolarSystem = SolarSystem;
}(window));